<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Controller\Admin;

use App\Service\AdminUserService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Annotation\PutMapping;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
use Psr\Http\Message\ResponseInterface;

/**
 * 管理者
 * Class ManagerController.
 */
#[Controller(prefix: 'admin/manager')]
class ManagerController extends MainController
{
    #[Inject]
    protected AdminUserService $adminUserService;

    #[Inject]
    protected ValidatorFactoryInterface $validationFactory;

    /**
     * 管理列表.
     */
    #[GetMapping('managerList')]
    public function managerList(): ResponseInterface
    {
        $data = $this->adminUserService->getManagerList($this->request->all());

        return $this->success($data);
    }

    /**
     * 创建管理员.
     */
    #[PostMapping('create')]
    public function create(RequestInterface $request): ResponseInterface
    {
        $validator = $this->validationFactory->make(
            $request->all(),
            [
                'username' => 'required',
                'password' => 'required',
                'name' => 'required',
            ],
            [
                'username.required' => 'username is required',
                'password.required' => 'password is required',
                'name.required' => 'name is required',
            ]
        );

        if ($validator->fails()) {
            $errorMessage = $validator->errors()->first();
            return $this->error($errorMessage);
        }

        $data = $request->inputs(['username', 'password', 'name', 'avatar']);
        $roleIds = (array)$request->input('role_ids', 0);

        if ($this->adminUserService->existUser($request->input('username'))) {
            return $this->error('username exists');
        }

        if ($this->adminUserService->create($data, $roleIds)) {
            return $this->success();
        }

        return $this->error('create fail');
    }

    /**
     * 更新.
     */
    #[PutMapping('update')]
    public function update(RequestInterface $request): ResponseInterface
    {
        $data = $request->inputs(['username', 'password', 'name', 'avatar', 'roleIds', 'status']);
        $id = (int)$request->input('id', 0);
        if ($this->adminUserService->update($id, $data)) {
            return $this->success();
        }

        return $this->error('update error');
    }
}
